﻿package com.PlantVsZombies.ui;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.image.BufferedImage;

import com.PlantVsZombies.Bullet.SunCollection;
import com.PlantVsZombies.Plants.SeedCard;
import com.PlantVsZombies.common.Life;
import com.PlantVsZombies.constant.Constant;
import com.PlantVsZombies.constant.PlantType;
import com.PlantVsZombies.util.ImageUtil;

/*
 * 
 * 植物框类，鼠标点击生产植物
 * 
 */

public class PlantsBar extends Life implements Constant{

	// 植物数量
	private int plantSum;
	// 植物框图片
	private BufferedImage seedBank;
	// 植物卡片
	private SeedCard[] cards;
	// 阳光值
	private int lights;
	// 字体
	private Font lightFont;
	
	private boolean shovelIsSelected = false;

	public static boolean isLive = true;
	
	public boolean isShovelIsSelected() {
		return shovelIsSelected;
	}

	public void setShovelIsSelected(boolean shovelIsSelected) {
		this.shovelIsSelected = shovelIsSelected;
	}

	// 创建植物种子列表
	public PlantsBar() {

		seedBank = ImageUtil.seedbank;
		plantSum = 7;
		cards = new SeedCard[plantSum];
		for (int i = 0; i < plantSum; ++i) {
			cards[i] = new SeedCard(new Point(SEED_OFFSET + ADD_SUN_OFFSET
					+ (CARD_WIDTH + CARD_GAP_W) * i, TOP_OFFSET), new Point(i,
					0));
		}
		// 设置阳光值的显示字体和最大值
		lightFont = new Font(Font.DIALOG, Font.BOLD, 20);
		lights = 50;
	}

	public void seedUpdate() {
		for (SeedCard sc : cards) {
			if (!sc.isEnable())
				sc.seedUpdate();
		}
	}

	// 获取所选植物类型
	public PlantType selectedPlant(Point pos) {
		if ((pos.x > SEED_OFFSET + ADD_SUN_OFFSET)
				&& (pos.x < SEED_OFFSET + ADD_SUN_OFFSET
						+ seedBank.getWidth(null)) && (pos.y > TOP_OFFSET)
				&& (pos.y < TOP_OFFSET + seedBank.getHeight(null))) {
			for (SeedCard sc : cards) {
				if (sc.mouseIn(pos.x, pos.y) && sc.isEnable()) {
					if (lights >= sc.getCost()) {
						sc.reset();
						lights -= sc.getCost();
						return sc.getPlantType();
					}
				}
			}
			if (pos.x > 550 && pos.x < 600 && pos.y > 0 && pos.y < 60) {
				shovelIsSelected = true;
				return PlantType.SHOVEL;
			}
			return PlantType.NONE;
		} else {
			return PlantType.NONE;
		}
	}

	public void draw(Graphics g) {
		g.drawImage(seedBank, Constant.SEED_OFFSET, 0, null);
		for (SeedCard sc : cards) {
			sc.draw(g);
		}
		// 画铁锹
		if (!shovelIsSelected) {
			g.drawImage(ImageUtil.shovel, 560, 5, 50, 50,  null);
		}
		g.setColor(Color.black);
		g.setFont(lightFont);
		g.drawString(String.valueOf(lights), SEED_OFFSET
				+ ADD_SUN_COUNT_X_OFFSET, ADD_SUN_COUNT_y_OFFSET);

	}

	// 收集阳光
	public void collectSun(int num) {
		lights += num;
	}

	// 若当前卡片不可使用，则显示黑色状态
	public void setCard() {
		for (int i = 0; i < cards.length; i++) {
			if (cards[i].getCost() > lights && cards[i].getCount() == 0) {
				cards[i].setPercent(0);
			} else {
				cards[i].setPercent(1);
			}
		}
	}

	@Override
	public int doRun() {
		setCard();
		seedUpdate();
		try {
			Thread.sleep(7);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		if(GamePanel.isPass || GamePanel.isGameOver){
			return 0;
		}
		return 1;
	}
}
